﻿namespace AnyOffice.general.workflow
{
    using AnyOffice.BIZ;
    using AnyOffice.DAL;
    using AnyOffice.WorkFlowRegex;
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Web;
    using AnyOffice.Components;
    using System.Reflection;
 

    public class FormPrint
    {
        private SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private string GetTimeElapsed(TimeSpan span)
        {
            string str = "";
            if (span.Days != 0) str = str + span.Days.ToString() + "天";
            if (span.Hours != 0) str = str + span.Hours.ToString() + "小时";
            if (span.Minutes != 0) str = str + span.Minutes.ToString() + "分";
            return (str + span.Seconds.ToString() + "秒");
        }

        public string Print(int runId, string prinFlag)
        {
            string str;
            this.conn.Open();
            WorkFlowBiz biz = new WorkFlowBiz();
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            string str2 = string.Empty;
            DataTable table = new FlowRunDao().SelectByRunId(this.conn, runId);
            if (table.Rows.Count > 0)
            {
                table.Rows[0]["RunName"].ToString();
                str2 = table.Rows[0]["AttachmentId"].ToString();
                if (table.Rows[0]["SignData2"] != DBNull.Value) flag = true;
            }
            if (prinFlag.IndexOf("1") > -1 || prinFlag.IndexOf("0") > -1)
            {
                FormRegex regex = new FormRegex {
                    RunId = runId,
                    RunName = table.Rows[0]["RunName"].ToString(),
                    RunDateTime = DateTime.Parse(table.Rows[0]["AddTime"].ToString())
                };
                ArrayList list = new ArrayList();
                DataTable table2 = new FlowRunDataDao().SelectByRunId(this.conn, runId);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        list.Add((table2.Rows[i]["ItemData"] != DBNull.Value) ? table2.Rows[i]["ItemData"].ToString() : "");
                    }
                }
                regex.SetValue = list;
                DataTable table3 = new FlowFormTypeDao().SelectByRunId(this.conn, runId);
                if (table3.Rows.Count > 0)
                {
                    regex.FormName = table3.Rows[0]["FormName"].ToString();
                    if (table3.Rows[0]["FormModel"] != DBNull.Value) regex.HtmlSource = table3.Rows[0]["FormModel"].ToString();
                }
                regex.HiddenItem = biz.HiddenItem(this.conn, runId, HttpContext.Current.User.Identity.Name);
                builder.Append(regex.GetHtmlView().Replace(@"\r\n", ""));
                if (flag)
                {
                    string str3 = string.Empty;
                    string str4 = string.Empty;
                    ArrayList title = regex.Title;
                    for (int j = 0; j < title.Count; j++)
                    {
                        if (j == 0)
                            str3 = str3 + title[j].ToString() + "=DATA_" + Convert.ToString((int) (j + 1));
                        else
                        {
                            string str5 = str3;
                            str3 = str5 + ";" + title[j].ToString() + "=DATA_" + Convert.ToString((int) (j + 1));
                        }
                    }
                    builder.Append("\n");
                    ArrayList list3 = list;
                    for (int k = 0; k < list3.Count; k++)
                    {
                        string str6 = str4;
                        str4 = str6 + "<input type=\"hidden\" id=\"DATA_" + Convert.ToString((int) (k + 1)) + "\" name=\"DATA_" + Convert.ToString((int) (k + 1)) + "\" value=\"" + list3[k].ToString() + "\">\n";
                    }
                    builder.Append(str4);
                    builder.Append("<script src=\"/lib/ntkoWebSign/NtkoAddSecSign.js\"></script>\n");
                    builder.Append("<script src=\"/lib/ntkoWebSign/ntkoGenObj.js\"></script>\n");
                    builder.Append("<script type=\"text/javascript\">gensecsign();</script>\n");
                    builder.Append("<script language=\"javascript\">\n");
                    builder.Append("function initWebSign()\n");
                    builder.Append("{\n");
                    builder.Append("  try{\n");
                    builder.Append("  var ntkoobj=null;\n");
                    builder.Append("  var webSignInfo=\"" + str3 + "\";\n");
                    builder.Append("  ntkoobj=document.getElementById(\"NTKO_SECSIGN_OCX\");\n");
                    builder.Append("  if(ntkoobj)\n");
                    builder.Append("      ntkoobj.LoadFromURL(\"/general/workflow/readsign.aspx?id=" + runId.ToString() + "\");\n");
                    builder.Append("  }catch (e) {}\n");
                    builder.Append("}\n");
                    builder.Append("window.onload = initWebSign;\n");
                    builder.Append("</script>\n");
                }
            }
            builder.Append("<iframe id=\"download_attach\" name=\"download_attach\" width=\"0\" height=\"0\"></iframe><br>\n");
            string str7 = ".docx.xlsx.pptx.ace.app.arj.asp.av.gif.avi.bmp.cab.chm.com.css.dll.doc.down.exe.gif.htm.html.img.jpeg.jpg.js.m3u.mdb.mid.mov.mp3.mpeg.mpg.pdf.ppt.ra.ram.rar.rm.sql.swf.tar.txt.url.wav.wmv.xls.zip.";
            string str8 = ".doc.xls.ppt.docx.xlsx.pptx";
            string str9 = "";
            if ((prinFlag.IndexOf("2") > -1 || prinFlag.IndexOf("0") > -1) && str2 != string.Empty)
            {
                AttachmentBiz.Add(str2);
                DataTable table4 = new AttachmentDao().SelectByAttachmentId(this.conn, str2);
                if (table4.Rows.Count > 0)
                {
                    builder.Append("<br>\n");
                    builder.Append("<TABLE class=small cellSpacing=1 cellPadding=3 width=100% align=center bgColor=#000000 border=0>\n");
                    builder.Append("<tr class=TableHeader>\n");
                    builder.Append("    <td>\n");
                    builder.Append("    <img src=/images/green_arrow.gif align=absMiddle>\n");
                    builder.Append("    公共附件区\n");
                    builder.Append("    </td>\n");
                    builder.Append("</tr>\n");
                    int num4 = 1;
                    for (int m = 0; m < table4.Rows.Count; m++)
                    {
                        if (m % 2 == 0)
                            num4 = 1;
                        else
                            num4 = 2;
                        builder.Append("<tr class=TableLine" + num4.ToString() + ">\n");
                        builder.Append("    <td>\n");
                        str = table4.Rows[m]["Name"].ToString();
                        if (str.LastIndexOf(".") > 0) str9 = str.Substring(str.LastIndexOf(".")).ToLower();
                        if (str7.IndexOf(str9 + ".") > -1) builder.Append("<img src=/images/files/" + str9.Replace(".", "") + ".gif align=absmiddle>");
                        builder.Append("<a href=\"/module/attach/download.aspx?attachment_id=" + table4.Rows[m]["AttachmentId"].ToString() + "&name=" + HttpContext.Current.Server.UrlEncode(table4.Rows[m]["Name"].ToString()) + "\" target=\"download_attach\">");
                        builder.Append(table4.Rows[m]["Name"].ToString());
                        builder.Append("</a>\n");
                        if (str8.IndexOf(str9) > -1) builder.Append("<input type=\"button\" value=\"阅读\" class=\"SmallButton\" onClick=\"window.open('/module/oc/index.aspx?attachment_id=" + table4.Rows[m]["AttachmentId"].ToString() + "&name=" + HttpContext.Current.Server.UrlEncode(table4.Rows[m]["Name"].ToString()) + "&op=0',null,'menubar=0,toolbar=0,status=1,scrollbars=1,resizable=1');\">\n");
                        builder.Append("\t</td>");
                        builder.Append("</tr>");
                    }
                    builder.Append("</table>");
                }
            }
            if (prinFlag.IndexOf("3") > -1 || prinFlag.IndexOf("0") > -1)
            {
                DataTable table5 = new FlowRunFeedbackDao().SelectByRunId(this.conn, runId);
                if (table5.Rows.Count > 0)
                {
                    AttachmentDao dao = new AttachmentDao();
                    string str10 = string.Empty;
                    builder.Append("<br>\n");
                    builder.Append("<TABLE class=small cellSpacing=1 cellPadding=3 width=100% align=center bgColor=#000000 border=0>\n");
                    builder.Append("<tr class=TableHeader>\n");
                    builder.Append("    <td>\n");
                    builder.Append("    <img src=/images/green_arrow.gif align=absMiddle>\n");
                    builder.Append("    会签意见区\n");
                    builder.Append("    </td>\n");
                    builder.Append("</tr>\n");
                    int num6 = 1;
                    string str11 = string.Empty;
                    for (int n = 0; n < table5.Rows.Count; n++)
                    {
                        if (n % 2 == 0)
                            num6 = 1;
                        else
                            num6 = 2;
                        builder.Append("<tr class=\"TableLine" + num6.ToString() + "\">\n");
                        builder.Append("<td>\n");
                        builder.Append("<strong>第" + table5.Rows[n]["PrcsId"].ToString() + "步 ");
                        if (table5.Rows[n]["PrcsName"] != DBNull.Value) builder.Append(table5.Rows[n]["PrcsName"].ToString());
                        builder.Append(" <u title=\"部门：" + table5.Rows[n]["DeptName"].ToString() + "\" style=\"cursor:hand\">" + table5.Rows[n]["UserName"].ToString() + "</u>：</strong><br>");
                        if (table5.Rows[n]["Content"] != DBNull.Value)
                        {
                            str11 = table5.Rows[n]["Content"].ToString().Replace("\r\n", "<br>").Replace(" ", "&nbsp;");
                            builder.Append(str11);
                        }
                        builder.Append("&nbsp;&nbsp;<i>" + table5.Rows[n]["EditTime"].ToString() + "</i><br>\n");
                        if (table5.Rows[n]["AttachmentId"] != DBNull.Value)
                        {
                            str10 = table5.Rows[n]["AttachmentId"].ToString();
                            AttachmentBiz.Add(str10);
                            DataTable table6 = dao.SelectByAttachmentId(this.conn, str10);
                            for (int num8 = 0; num8 < table6.Rows.Count; num8++)
                            {
                                str = table6.Rows[num8]["Name"].ToString();
                                if (str.LastIndexOf(".") > 0) str9 = str.Substring(str.LastIndexOf(".")).ToLower();
                                if (str7.IndexOf(str9 + ".") > -1) builder.Append("<img src=\"/images/files/" + str9.Replace(".", "") + ".gif\" align=\"absmiddle\">");
                                builder.Append("<a href=\"/module/attach/download.aspx?attachment_id=" + table6.Rows[num8]["AttachmentId"].ToString() + "&name=" + HttpContext.Current.Server.UrlEncode(table6.Rows[num8]["Name"].ToString()) + "\" target=\"download_attach\">");
                                builder.Append(table6.Rows[num8]["Name"].ToString());
                                builder.Append("</a>\n");
                                if (str8.IndexOf(str9) > -1) builder.Append("<input type=\"button\" value=\"阅读\" class=\"SmallButton\" onClick=\"window.open('/module/oc/index.aspx?attachment_id=" + table6.Rows[num8]["AttachmentId"].ToString() + "&name=" + HttpContext.Current.Server.UrlEncode(table6.Rows[num8]["Name"].ToString()) + "&op=0',null,'menubar=0,toolbar=0,status=1,scrollbars=1,resizable=1');\">\n");
                                builder.Append("<br>");
                            }
                        }
                        builder.Append("\t</td>");
                        builder.Append("</tr>");
                    }
                    builder.Append("</table>");
                }
            }
            if (prinFlag.IndexOf("4") > -1 || prinFlag.IndexOf("0") > -1)
            {
                DataTable table7 = biz.View1(this.conn, runId);
                if (table7.Rows.Count > 0)
                {
                    builder.Append("<br>\n");
                    builder.Append("<TABLE class=small cellSpacing=1 cellPadding=3 width=100% align=center bgColor=#000000 border=0>\n");
                    builder.Append("<tr class=TableHeader>\n");
                    builder.Append("    <td align=center colSpan=3>流程图</td>\n");
                    builder.Append("</tr>\n");
                    for (int num9 = 0; num9 < table7.Rows.Count; num9++)
                    {
                        builder.Append("<tr class=TableLine1>\n");
                        builder.Append("<td nowrap align=center>第<b><span class=Big4>" + table7.Rows[num9]["PrcsId"].ToString() + "</span></b>步</td>\n");
                        if (Convert.ToInt32(table7.Rows[num9]["FlowType"].ToString()) == 1)
                        {
                            builder.Append("<td>\n");
                            builder.Append("<img src=/images/arrow_down.gif>");
                            builder.Append(" 序号" + table7.Rows[num9]["FlowPrcs"].ToString() + "：" + table7.Rows[num9]["PrcsName"].ToString());
                            builder.Append("</td>\n");
                        }
                        builder.Append("<td>\n");
                        DataView defaultView = biz.View2(this.conn, runId, Convert.ToInt32(table7.Rows[num9]["PrcsId"].ToString())).DefaultView;
                        if (defaultView.Count > 0)
                        {
                            for (int num10 = 0; num10 < defaultView.Count; num10++)
                            {
                                TimeSpan span;
                                if (num10 != 0) builder.Append("<br>");
                                switch (Convert.ToInt32(defaultView[num10]["PrcsFlag"].ToString()))
                                {
                                    case 1:
                                        builder.Append("<img src='/images/email_close.gif' alt='未接收' align='absmiddle'>  ");
                                        break;

                                    case 2:
                                        builder.Append("<img src='/images/email_open.gif' alt='已接收' align='absmiddle'>  ");
                                        break;

                                    case 3:
                                        builder.Append("<img src='/images/flow_next.gif' alt='已办结' align='absmiddle'>  ");
                                        break;

                                    case 4:
                                        builder.Append("<img src='/images/flow_next.gif' alt='已办结' align='absmiddle'>  ");
                                        break;
                                }
                                if (Convert.ToInt32(defaultView[num10]["OpFlag"].ToString()) == 1)
                                    builder.Append("<span class='big4'><u title='主办人，部门：" + defaultView[num10]["DeptName"].ToString() + "' style='cursor:hand'><b>" + defaultView[num10]["UserName"].ToString() + "</b></u></span>");
                                else
                                    builder.Append("<u title='部门：" + defaultView[num10]["DeptName"].ToString() + "' style='cursor:hand'><b>" + defaultView[num10]["UserName"].ToString() + "</b></u>");
                                switch (Convert.ToInt32(defaultView[num10]["PrcsFlag"].ToString()))
                                {
                                    case 1:
                                        builder.Append(" [<font color=green>未接收办理</font>]");
                                        if (defaultView[num10]["TimeOut"] != DBNull.Value)
                                        {
                                            string str12 = "SELECT TOP 1 [DeliverTime] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = " + defaultView[num10]["RunId"].ToString() + " AND [PrcsId] <" + defaultView[num10]["PrcsId"].ToString() + " ORDER BY [PrcsId] ASC";
                                            object obj2 = SqlHelper.ExecuteScalar(this.conn, CommandType.Text, str12);
                                            if (obj2 != null)
                                            {
                                                DateTime time = Convert.ToDateTime(obj2);
                                                if (time.AddHours((double) Convert.ToInt32(defaultView[num10]["TimeOut"].ToString())) < Convert.ToDateTime(defaultView[num10]["NowTime"].ToString()))
                                                {
                                                    builder.Append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>限时" + defaultView[num10]["TimeOut"].ToString() + "小时,超时");
                                                    builder.Append(this.GetTimeElapsed((TimeSpan) (Convert.ToDateTime(defaultView[num10]["NowTime"].ToString()) - time)));
                                                    builder.Append("</font>");
                                                }
                                            }
                                        }
                                        break;

                                    case 2:
                                        builder.Append(" [<font color=green>办理中");
                                        if (defaultView[num10]["PrcsTime"] != DBNull.Value)
                                        {
                                            span = (TimeSpan) (Convert.ToDateTime(defaultView[num10]["NowTime"].ToString()) - Convert.ToDateTime(defaultView[num10]["PrcsTime"].ToString()));
                                            builder.Append("&nbsp;已用时：" + this.GetTimeElapsed(span));
                                        }
                                        builder.Append("</font>]");
                                        if (defaultView[num10]["TimeOut"] != DBNull.Value && Convert.ToDateTime(defaultView[num10]["PrcsTime"].ToString()).AddHours((double) Convert.ToInt32(defaultView[num10]["TimeOut"].ToString())) < Convert.ToDateTime(defaultView[num10]["NowTime"].ToString()))
                                        {
                                            builder.Append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>限时" + defaultView[num10]["TimeOut"].ToString() + "小时,超时");
                                            builder.Append(this.GetTimeElapsed((TimeSpan) (Convert.ToDateTime(defaultView[num10]["NowTime"].ToString()) - Convert.ToDateTime(defaultView[num10]["PrcsTime"].ToString()))));
                                            builder.Append("</font>");
                                        }
                                        break;

                                    case 3:
                                        builder.Append(" [<font color=green>已转交下一步&nbsp;用时：");
                                        if (defaultView[num10]["PrcsTime"] != DBNull.Value && defaultView[num10]["DeliverTime"] != DBNull.Value)
                                        {
                                            span = (TimeSpan) (Convert.ToDateTime(defaultView[num10]["DeliverTime"].ToString()) - Convert.ToDateTime(defaultView[num10]["PrcsTime"].ToString()));
                                            builder.Append(this.GetTimeElapsed(span) ?? "");
                                        }
                                        builder.Append("</font>]");
                                        break;

                                    case 4:
                                        builder.Append(" [<font color=green>已办结&nbsp;用时：");
                                        if (defaultView[num10]["PrcsTime"] != DBNull.Value && defaultView[num10]["DeliverTime"] != DBNull.Value)
                                        {
                                            span = (TimeSpan) (Convert.ToDateTime(defaultView[num10]["DeliverTime"]) - Convert.ToDateTime(defaultView[num10]["PrcsTime"]));
                                            builder.Append(this.GetTimeElapsed(span) ?? "");
                                        }
                                        builder.Append("</font>]");
                                        break;
                                }
                                if (Convert.ToInt32(defaultView[num10]["PrcsFlag"].ToString()) > 1)
                                {
                                    builder.Append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;开始于：");
                                    if (defaultView[num10]["PrcsTime"] != DBNull.Value) builder.Append(defaultView[num10]["PrcsTime"].ToString());
                                    if (defaultView[num10]["DeliverTime"] != DBNull.Value)
                                    {
                                        builder.Append("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;步骤结束于：");
                                        builder.Append(defaultView[num10]["DeliverTime"].ToString());
                                    }
                                }
                            }
                        }
                        builder.Append("</td>\n");
                        builder.Append("</tr>\n");
                    }
                    builder.Append("</table>\n");
                }
            }
            this.conn.Close();
            return builder.ToString();
        }
    }
}

